package com.enjoy.studyfragment.fragment;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.enjoy.studyfragment.R;
import com.enjoy.studyfragment.adapter.CircleFriendsAdapter;
import com.enjoy.studyfragment.util.PictureDialogUtil;
import com.enjoy.studyfragment.widget.FullyGridLayoutManager;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 朋友圈效果
 * Created by Administrator on 2018/3/16.
 */

public class CircleFriendsFragment extends TopBarFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<LocalMedia> selectList = new ArrayList<>();//拍照或相册选择图片集合
    private CircleFriendsAdapter adapter;

    @Override
    public Object setLayout() {
        return R.layout.fragment_circle_friends;
    }

    @Override
    public void init() {
        super.init();
        initRecycler();
    }

    public void initRecycler(){
        FullyGridLayoutManager manager = new FullyGridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new CircleFriendsAdapter(getContext());
        adapter.setNewData(selectList);
        adapter.setSelectMax(9);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new CircleFriendsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                // 预览图片
                PictureSelector.create(CircleFriendsFragment.this).externalPicturePreview(position, selectList);
                Toast.makeText(mActivity, "aa", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFirstItemClick() {
                PictureDialogUtil.showPicture(getActivity(),CircleFriendsFragment.this,selectList);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) { //拍照或相册选择返回事件
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择
                    selectList = PictureSelector.obtainMultipleResult(data);
                    adapter.setNewData(selectList);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    }
}
